import re
class CheckUtils:
    def __init__(self, type, infos):
        self.type = type
        self.info = infos

    def check(self):
        if type == 'register':
            name = self.infos.get('name')
            uid = self.infos.get('uid')
            school = self.infos.get('school')
            classname = self.infos.get('classname')
            age = self.infos.get('age')
            phone = self.infos.get('phone')
            email = self.infos.get('email')
            user = self.infos.get('user')
            passwordInfo = self.infos.get('passwordInfo')
            if name == '' or not name:
                return {"code": 0, "msg": "姓名不能为空"}
            if uid == '' or not uid:
                return {"code": 0, "msg": "学号不能为空"}
            else:
                pat = re.compile('^[0-9]+$', re.S)
                res = re.findall(pat, uid)
                if len(res) == 0:
                    return {"code": 0, "msg": "学号只能为数字"}
            if school == '' or not school:
                return {"code": 0, "msg": "学院不能为空"}
            if classname == '' or not classname:
                return {"code": 0, "msg": "班级不能为空"}
            if age == '' or not age:
                return {"code": 0, "msg": "年龄不能为空"}
            else:
                pat1 = re.compile('^1[7-9]$', re.S)
                pat2 = re.compile('^2[0-2]$', re.S)
                res1 = re.findall(pat1, age)
                res2 = re.findall(pat2, age)
                if len(res1 or res2) == 0:
                    return {"code": 0, "msg": "年龄应在17~22之间"}
            if phone == '' or not phone:
                return {"code": 0, "msg": "电话不能为空"}
            else:
                pat = re.compile('^1[3456789][0-9]{9}$', re.S)
                res = re.findall(pat, phone)
                if len(res) == 0:
                    return {"code": 0, "msg": "电话不合法"}
            if email == '' or not email:
                return {"code": 0, "msg": "email不能为空"}
            else:
                pat = re.compile('^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$', re.S)
                res = re.findall(pat, email)
                if len(res) == 0:
                    return {"code": 0, "msg": "email不合法"}
            if user == '' or not user:
                return {"code": 0, "msg": "用户名不能为空"}
            else:
                if len(user) < 6:
                    return {"code": 0, "msg": "用户名至少包含字母大小写、数字、下划线中的三种，字母开始，长度至少6位"}
                count = 0
                pat1 = re.compile('([a-z]+)', re.S)
                pat2 = re.compile('([A-Z]+)', re.S)
                pat3 = re.compile('([0-9]+)', re.S)
                pat4 = re.compile('([_]+)', re.S)
                if len(re.findall(pat1, user)) != 0: count += 1
                if len(re.findall(pat2, user)) != 0: count += 1
                if len(re.findall(pat3, user)) != 0: count += 1
                if len(re.findall(pat4, user)) != 0: count += 1
                if not len(re.findall(pat1, user[0])) and not len(re.findall(pat2, user[0])):
                    return {"code": 0, "msg": "用户名至少包含字母大小写、数字、下划线中的三种，字母开始，长度至少6位"}
                if count<3:
                    return {"code": 0, "msg": "用户名至少包含字母大小写、数字、下划线中的三种，字母开始，长度至少6位"}
            if passwordInfo == '' or not passwordInfo:
                return {"code": 0, "msg": "密码不能为空"}
            else:
                if len(passwordInfo) < 8:
                    return {"code": 0, "msg": "密码至少包含字母大小写、数字、下划线中的三种，字母开始，长度至少6位"}
                count = 0
                pat1 = re.compile('([a-z]+)', re.S)
                pat2 = re.compile('([A-Z]+)', re.S)
                pat3 = re.compile('([0-9]+)', re.S)
                pat4 = re.compile('([_]+)', re.S)
                if len(re.findall(pat1, passwordInfo)) != 0: count += 1
                if len(re.findall(pat2, passwordInfo)) != 0: count += 1
                if len(re.findall(pat3, passwordInfo)) != 0: count += 1
                if len(re.findall(pat4, passwordInfo)) != 0: count += 1
                if count < 3:
                    return {"code": 0, "msg": "密码至少包含字母大小写、数字、下划线中的三种，字母开始，长度至少6位"}
            return {"code": 1}
        elif type == 'login':
            user = self.infos.get('user')
            pwd = self.infos.get('passwordInfo')
            if user == '' or not user:
                return {"code": 0, "msg": "用户名不能为空"}
            if pwd == '' or not pwd:
                return {"code": 0, "msg": "密码不能为空"}
            return {"code": 1}
        else:
            return {"code": 0, "msg": "请求错误"}
